package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.dao.MedicationDao;
import com.HospitalOutpatient.entity.Medication;
import com.HospitalOutpatient.service.MedicationService;
import com.HospitalOutpatient.vo.MedicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("medicationService")
public class MedicationServiceImpl implements MedicationService {

    @Autowired
    private MedicationDao medicationDao;

    @Override
    public List<Medication> getAllMedication() {
        return medicationDao.selectAllMedication();
    }

    @Override
    public boolean addMedication(Medication medication) {
        return medicationDao.insertMedication(medication) == 1;
    }

    @Override
    public boolean deleteMedication(String medicationId) {
        return medicationDao.deleteMedication(medicationId) == 1;
    }

    @Override
    public List<Medication> getMedicationByName(String medicationName) {
        if(medicationName != null && medicationName  !=""){
            medicationName = "%"+medicationName+"%";
        }
        return medicationDao.selectMedicationByName(medicationName);
    }

    @Override
    public List<MedicationVo> getMedicationVo() {

        List<Medication> medicationList = medicationDao.selectAllMedication();

        List<MedicationVo> medicationVoList = new ArrayList<>();

        for (Medication medication : medicationList) {
            MedicationVo medicationVo = new MedicationVo();

            medicationVo.setValue(medication.getMedicationId());
            medicationVo.setLabel(medication.getMedicationName());

            medicationVoList.add(medicationVo);
        }
        return medicationVoList;
    }
}
